/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bill.model.dao;

import bill.model.vo.Inventory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.DBUtils;

/**
 *
 * @author Administrator
 */
public class InventoryDAO {

   public static ArrayList<Inventory> getInventories(Inventory inventory){
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ArrayList<Inventory> inventories = new ArrayList<Inventory>();
        String id = null;
        if(inventory != null && inventory.getId() != null){
            id = inventory.getId();
        }
        String name = null;
        if(inventory != null && inventory.getName() != null){
            name = inventory.getName();
        }
        try {
            con = DBUtils.getDataConnection();
            String sql = "select cInvCode,cInvName,cInvStd,bFree1,iGroupType," +
                    " cGroupCode,cComUnitCode,cSAComUnitCode,cInvCCode from Inventory";
            if (inventory != null) {
                if (id != null) {
                    sql += " where cInvCode like ?";
                } else if (name != null) {
                    sql += " where cInvName like ?";
                }
            }
            pstmt = con.prepareStatement(sql);
            if(inventory != null){
                if(id != null){
                    pstmt.setString(1, id);
                }else if(name != null){
                    pstmt.setString(1, name);
                }
            }
            rs = pstmt.executeQuery();
            int num = 0;
            while(rs.next()){
                Inventory inv = new Inventory(++num,rs.getString(1),rs.getString(2),rs.getString(3),rs.getInt(4),
                        rs.getByte(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9));
                inventories.add(inv);
            }            
        } catch (SQLException ex) {
            Logger.getLogger(CustomerDAO.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            DBUtils.closeAll(rs, pstmt, con);
        }
        return inventories;
    }
}
